const START_LEVEL = 4

const viewUpdators = []

const user = {
  level: START_LEVEL
}

/**
 * 攻击玩家
 * return 是否死亡
 */
const hitted = function (hurm = 1) {
  user.level-- // 玩家死亡，等级-1
  if (user.level < START_LEVEL) {
    user.level = START_LEVEL  // 不能低于起始等级
  }
  
  return true
}

/**
 * 奖励玩家
 */
const reward = function () {
  user.level++ // 玩家获得奖励，等级-1
}

const getLevel = function () {
  return user.level
}

const flushUserInfo = function() {
  _updateView()
}

const bindView = function (f) {
  f(user)
  viewUpdators.push(f);
}

const _updateView = function () {
  for (let i = 0; i < viewUpdators.length; i++) {
    viewUpdators[i](user)
  }
}

module.exports = {
  hitted,
  reward,
  getLevel,
  flushUserInfo,
  bindView
}